package com.chenglai.goods.controller;

import com.chenglai.commonutils.common.Result;
import com.chenglai.commonutils.common.StatusCode;
import com.chenglai.goods.pojo.ProductAnswer;
import com.chenglai.goods.service.AnswerService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author jiang
 * @created 2023/11/16 14:27:54
 **/
@CrossOrigin
@RestController
@RequestMapping("/answer")
@Api(value = "/answer", tags = {"商品问答服务"})
public class AnswerController {

    @Autowired
    private AnswerService answerService;

    @GetMapping("/addAnswerComment")
    @ApiOperation(value = "添加商品问答")
    public Result addAnswerComment(ProductAnswer productAnswer){
        try {
            answerService.addProductCommentPic(productAnswer);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @GetMapping("/getAnswerComment")
    @ApiOperation(value = "获取商品问答")
    public Result getAnswerComment(@ApiParam(name = "productId", value = "商品ID") @RequestParam("productId")Integer productId){
        try {
            List<ProductAnswer> productAnswerList = answerService.getAnswerComment(productId);
            return new Result(true, StatusCode.OK,"操作成功",productAnswerList);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }
    @GetMapping("/delAnswerComment")
    @ApiOperation(value = "用户删除商品问答")
    public Result delAnswerComment(@ApiParam(name = "answerId", value = "answerId") @RequestParam("answerId")Integer answerId){
        try {
            answerService.delAnswerComment(answerId);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @PostMapping("/getAnswerComment")
    @ApiOperation(value = "后台获取问答列表")
    public Result getSlideshowList(@ApiParam(name = "page", value = "page 页") @RequestParam("page")Integer page,
                                   @ApiParam(name = "size", value = "size 条") @RequestParam("size")Integer size,
                                   @ApiParam(name = "shopId", value = "shopId 门店Id") @RequestParam("shopId")Integer shopId){
        try {
            PageInfo pageInfo = answerService.getSlideshowList(page,size,shopId);
            return new Result(true, StatusCode.OK,"操作成功",pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

    @PostMapping("/updateAnswerComment")
    @ApiOperation(value = "审核问答状态")
    public Result updateAnswerComment(@ApiParam(name = "answerId", value = "answerId") @RequestParam("answerId")Integer answerId,
                                      @ApiParam(name = "answerStatus", value = "0未审核 1已通过") @RequestParam("answerStatus")Integer answerStatus){
        try {
            answerService.updateAnswerComment(answerId,answerStatus);
            return new Result(true, StatusCode.OK,"操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, StatusCode.ERROR,"操作失败");
        }
    }

}
